package tech.wetech.admin3.sys.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.wetech.admin3.sys.model.AooCompCertiData;
import tech.wetech.admin3.sys.model.AooPersCertiData;
import tech.wetech.admin3.sys.model.AooUser;
import tech.wetech.admin3.sys.repository.AooPersCertiDataRepository;
import tech.wetech.admin3.sys.repository.AooUserRepository;

import java.util.List;


@Service
public class AooPersCertiDataService {
  private AooPersCertiDataRepository persCertiDataRepository;
  private AooUserRepository aooUserRepository;

  public AooPersCertiDataService(AooPersCertiDataRepository aooPersCertiDataRepository, AooUserRepository aooUserRepository) {
    this.persCertiDataRepository = aooPersCertiDataRepository;
    this.aooUserRepository = aooUserRepository;
  }

  @Transactional
  public AooPersCertiData upload(AooPersCertiData aooPersCertiData) {

    return persCertiDataRepository.save(aooPersCertiData);
  }


  @Transactional
  public boolean resetStatus(String email, Integer newStatus, String checkDesc) {
    AooUser aooUser = aooUserRepository.findByEmail(email);
    AooPersCertiData persCertiData = persCertiDataRepository.findByEmail(email);
    if (aooUser != null) {
      aooUser.setStatus(newStatus);
      aooUser.setCheckDesc(checkDesc);
      aooUser.setAuthType(1);
      aooUserRepository.save(aooUser);

      persCertiData.setStatus(newStatus);
      persCertiDataRepository.save(persCertiData);

      return true;
    } else {
      return false;
    }
  }

  public List<AooPersCertiData> getAllData() {
    return persCertiDataRepository.findAll();
  }

  public AooPersCertiData findByEmail(String email) {
    return persCertiDataRepository.findByEmail(email);
  }

  public void deleteByEmail(String email) {
    persCertiDataRepository.deleteByEmail(email);
  }
}
